{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Let's predict lottery numbers based on previous winning numbers!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, grab the previous lottery data from an api"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'totSellamnt': 88625160000,\n",
       " 'returnValue': 'success',\n",
       " 'drwNoDate': '2020-03-21',\n",
       " 'firstWinamnt': 1684582212,\n",
       " 'drwtNo6': 28,\n",
       " 'drwtNo4': 21,\n",
       " 'firstPrzwnerCo': 13,\n",
       " 'drwtNo5': 22,\n",
       " 'bnusNo': 45,\n",
       " 'firstAccumamnt': 21899568756,\n",
       " 'drwNo': 903,\n",
       " 'drwtNo2': 15,\n",
       " 'drwtNo3': 16,\n",
       " 'drwtNo1': 2}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "req_lot = requests.get(\"https://www.dhlottery.co.kr/common.do?method=getLottoNumber&drwNo=903\")\n",
    "req_lot.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "drwtNo1 = []\n",
    "drwtNo2 = []\n",
    "drwtNo3 = []\n",
    "drwtNo4 = []\n",
    "drwtNo5 = []\n",
    "drwtNo6 = []\n",
    "bnusNo = []\n",
    "drwNoDate = []\n",
    "\n",
    "for drwNo in range(1,948):\n",
    "    req_lot = requests.get(f\"https://www.dhlottery.co.kr/common.do?method=getLottoNumber&drwNo={str(drwNo)}\")\n",
    "    lotNo = req_lot.json()\n",
    "    drwtNo1.append(lotNo['drwtNo1'])\n",
    "    drwtNo2.append(lotNo['drwtNo2'])\n",
    "    drwtNo3.append(lotNo['drwtNo3'])\n",
    "    drwtNo4.append(lotNo['drwtNo4'])\n",
    "    drwtNo5.append(lotNo['drwtNo5'])\n",
    "    drwtNo6.append(lotNo['drwtNo6'])\n",
    "    bnusNo.append(lotNo['bnusNo'])\n",
    "    drwNoDate.append(lotNo['drwNoDate'])\n",
    "\n",
    "    lot_dict = {\"Date\":drwNoDate, \"Num1\":drwtNo1, \"Num2\":drwtNo2, \"Num3\":drwtNo3, \"Num4\":drwtNo4, \"Num5\":drwtNo5, \"Num6\":drwtNo6, \"bnsNum\":bnusNo}\n",
    "\n",
    "    df_lot = pd.DataFrame(lot_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_lot.to_csv(\"winning_nums\", index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Num1</th>\n",
       "      <th>Num2</th>\n",
       "      <th>Num3</th>\n",
       "      <th>Num4</th>\n",
       "      <th>Num5</th>\n",
       "      <th>Num6</th>\n",
       "      <th>bnsNum</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2002-12-07</td>\n",
       "      <td>10</td>\n",
       "      <td>23</td>\n",
       "      <td>29</td>\n",
       "      <td>33</td>\n",
       "      <td>37</td>\n",
       "      <td>40</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2002-12-14</td>\n",
       "      <td>9</td>\n",
       "      <td>13</td>\n",
       "      <td>21</td>\n",
       "      <td>25</td>\n",
       "      <td>32</td>\n",
       "      <td>42</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2002-12-21</td>\n",
       "      <td>11</td>\n",
       "      <td>16</td>\n",
       "      <td>19</td>\n",
       "      <td>21</td>\n",
       "      <td>27</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2002-12-28</td>\n",
       "      <td>14</td>\n",
       "      <td>27</td>\n",
       "      <td>30</td>\n",
       "      <td>31</td>\n",
       "      <td>40</td>\n",
       "      <td>42</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2003-01-04</td>\n",
       "      <td>16</td>\n",
       "      <td>24</td>\n",
       "      <td>29</td>\n",
       "      <td>40</td>\n",
       "      <td>41</td>\n",
       "      <td>42</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>942</th>\n",
       "      <td>2020-12-26</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>13</td>\n",
       "      <td>36</td>\n",
       "      <td>44</td>\n",
       "      <td>45</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>943</th>\n",
       "      <td>2021-01-02</td>\n",
       "      <td>2</td>\n",
       "      <td>13</td>\n",
       "      <td>16</td>\n",
       "      <td>19</td>\n",
       "      <td>32</td>\n",
       "      <td>33</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>944</th>\n",
       "      <td>2021-01-09</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>30</td>\n",
       "      <td>33</td>\n",
       "      <td>37</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>945</th>\n",
       "      <td>2021-01-16</td>\n",
       "      <td>9</td>\n",
       "      <td>18</td>\n",
       "      <td>19</td>\n",
       "      <td>30</td>\n",
       "      <td>34</td>\n",
       "      <td>40</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>946</th>\n",
       "      <td>2021-01-23</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>17</td>\n",
       "      <td>20</td>\n",
       "      <td>27</td>\n",
       "      <td>35</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>947 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           Date  Num1  Num2  Num3  Num4  Num5  Num6  bnsNum\n",
       "0    2002-12-07    10    23    29    33    37    40      16\n",
       "1    2002-12-14     9    13    21    25    32    42       2\n",
       "2    2002-12-21    11    16    19    21    27    31      30\n",
       "3    2002-12-28    14    27    30    31    40    42       2\n",
       "4    2003-01-04    16    24    29    40    41    42       3\n",
       "..          ...   ...   ...   ...   ...   ...   ...     ...\n",
       "942  2020-12-26     1     8    13    36    44    45      39\n",
       "943  2021-01-02     2    13    16    19    32    33      42\n",
       "944  2021-01-09     9    10    15    30    33    37      26\n",
       "945  2021-01-16     9    18    19    30    34    40      20\n",
       "946  2021-01-23     3     8    17    20    27    35      26\n",
       "\n",
       "[947 rows x 8 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"./winning_nums.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
